<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\api\controller;


use api\api\service\UserService;
use cmf\controller\RestBaseController;
use think\Db;

/** 代驾控制器
 * Class SubstituteController
 * @package api\api\controller
 */

class SubstituteController extends RestBaseController
{
	/**
	  * @api {post} get_substitute 获取地区代驾价格.
	  * @apiGroup Substitute
	  *
	  * @apiParam {Int} token 用户token.
	  *
	  * @apiSuccess {String} code 返回值.
	  * @apiSuccess {String} msg  返回说明.
	  * @apiSuccess {String} data  返回数据.
	  *
	  * @apiSuccessExample Success-Response:
	  *     HTTP/1.1 200 OK
	  *     {
	  *     	   "code": 1,
	  * 		   "msg": '获取成功'
	  *         "data": ""
	  *     }
	  *
	  * @apiError {String} code 返回值.
	  * @apiError {String} msg  返回说明.
	  *
	  * @apiErrorExample Error-Response:
	  *     HTTP/1.1 404 Not Found
	  *     {
	  *         "code": 0,
	  * 		   "msg": '获取失败'
	  *     }
	  */
	public function get_substitute($mileage,$adcode)
	{
	    $kilometre = number_format($mileage, 2);
        $twoStartKiloMoney = $this->getTwoStartKiloMoney($kilometre,$adcode);//返回起步价、里程费、总价等数组
        $totalPrice = $twoStartKiloMoney['totalPrice'];//总里程金额
        $data['bj_mileage_amount'] = format_money($totalPrice);
        $this->success('获取成功', $data);

	}

/**
* 两个起步价的订单金额计算
* @param $cityId float 城市
* @param $kilometers float 公里数
* 返回值；
* @param "startkilometres"  起始公里数
* @param startmoney" 起始金额
 * @param "totalPrice" 总里程金额
 * @param "money" 超起始价金额
 * @param"kilometres" 超起始公里数
 */
function getTwoStartKiloMoney($kilometers,$adcode)
{

    $kilometers = ceil($kilometers);
//    dump('公里数：');
    //dump($kilometers);

    $totalPrice = array();
//    $ret = Db::name('pricing_tactics')->where('pid', 1)->field('tactics_array')->find();
    $where['county_id'] = $adcode;
    $where['type'] = 0;
    $ret = Db::name('pricing_tactics')->where($where)->find();
    if ($ret['tactics_array_s']) {
        $tactics_array = json_decode($ret['tactics_array_s'], TRUE);
        $mileage = $tactics_array['mileage'];
        $starting_price = $tactics_array['is_starting_price'];

        //计算起步公里数及起步价
        foreach ($starting_price as $key => $value) {//起步价集合循环
            if ($kilometers <= round($value['mileageKilometre']) || $key == count($starting_price) - 1) {//起步价
                $totalPrice['startkilometres'] = $value['mileageKilometre'];//起步公里数
                $totalPrice['startmoney'] = $value['mileageMoney'];//起步价
                $totalPrice['totalPrice'] = $value['mileageMoney'];
                $totalPrice['money'] = 0.00;
                $totalPrice['kilometres'] = 0.00;
                break;
            }
        }
//        dump('totalPrice：');
//        dump($totalPrice);
        //计算里程公里数及里程费用
        $mileage_kilometre = 0.00;
        $mileage_money = 0.00;
        foreach ($mileage as $key1 => $value1) {//起步价集合循环
//            dump('循环'.$key1);
//            dump('kilometre = '. $kilometers);
//            dump('startkilometres = '.round($totalPrice['startkilometres']));
//            dump('value1-kilometre = '.round($value1['kilometre']));


            if ($kilometers > round($totalPrice['startkilometres']) && $kilometers <= round($value1['kilometre'])) {//小于等于起步价，则里程费0，否则计算里程费
                $mileage_money += $mileage[$key1 - 1]['money'];
//                dump($mileage_money);
                $mileage_kilometre += $kilometers - $mileage[$key1 - 1]['kilometre'];
//                dump($mileage_kilometre);
                $mileage_money += $mileage_kilometre * $mileage[$key1]['pricePer'];
//                dump($mileage_money);
                break;
            }
        }
//        exit();
//        dump($mileage_kilometre);
//        dump($mileage_money);
        if ($mileage_kilometre > 0) {
            $totalPrice['totalPrice'] = $mileage_money;
            $totalPrice['money'] = $mileage_money - $totalPrice['startmoney'];
        }
    }
    return $totalPrice;
}
}
